package com.example.dense_rack_server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dense_rack_server.entity.MyDept;
import com.example.dense_rack_server.entityVo.MenuALLVo;
import com.example.dense_rack_server.mapper.MyDeptMapper;
import com.example.dense_rack_server.utils.DateFormat;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-03
 */
@Service
public class MyDeptService extends ServiceImpl<MyDeptMapper, MyDept> {
    @Resource
    private MyDeptMapper myDeptMapper;

    /**
     * 部门表格
     */
    public Map<String, Object> deptList() {
        QueryWrapper<MyDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<MyDept> list = myDeptMapper.selectList(queryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "请求成功");
        resultMap.put("count", null);
        resultMap.put("data", list);
        return resultMap;
    }

    /**
     * 绘制部门树
     */
    public ResponseModel buildDeptAll() {
        List<MenuALLVo> sysUserVoList = myDeptMapper.buildDeptAll();
        return ResponseModel.success(sysUserVoList);
    }


    /**
     * 部门停用和启用
     */
    @ResponseBody
    public ResponseModel updateStatus(JSONObject data) {
        int updateStatus = myDeptMapper.update(null, new UpdateWrapper<MyDept>().eq("dept_id", data.getInt("deptId"))
                .set("status", data.getInt("status")));
        if (updateStatus > 0) {
            if (data.getInt("status") == 0) {
                return ResponseModel.success("部门已禁用");
            } else {
                return ResponseModel.success("部门已启用");
            }
        } else {
            return ResponseModel.fail("修改部门状态失败");
        }
    }

    /**
     * 新增部门保存
     */
    public ResponseModel addDept(JSONObject data) {
        QueryWrapper<MyDept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("dept_name", data.getStr("deptName"));
        MyDept myDept = myDeptMapper.selectOne(deptQueryWrapper);
        if (myDept != null) {
            return ResponseModel.fail("部门名称重复");
        }
        MyDept insertMyDept = new MyDept();
        if (StrUtil.isEmpty(data.getStr("parentId"))) {
            insertMyDept.setParentId(0);
        } else {
            insertMyDept.setParentId(data.getInt("parentId"));
        }
        insertMyDept.setDeptName(data.getStr("deptName"));
        insertMyDept.setSort(data.getInt("sort"));
        insertMyDept.setStatus(data.getInt("status"));
        insertMyDept.setCreateTime(DateFormat.getTime());
        insertMyDept.setUpdateTime(DateFormat.getTime());
//        insertMyDept.setDepartmentHeadUserId(data.getInt("userId"));
        int insertStatus = myDeptMapper.insert(insertMyDept);
        return insertStatus > 0 ? ResponseModel.success("新增  " + data.getStr("deptName") + "  部门成功") : ResponseModel.fail("新增部门失败");
    }

    /**
     * 修改部门信息保存
     *
     * @param data
     * @return
     */
    public ResponseModel editDept(JSONObject data) {
        UpdateWrapper<MyDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dept_id", data.getInt("deptId"))
                .set("parent_id", data.getInt("parentId"))
                .set("dept_name", data.getStr("deptName"))
                .set("sort", data.getInt("sort"))
                .set("status", data.getInt("status"));
//                .set("department_head_user_id", data.getInt("userId"));
        int updateStatus = myDeptMapper.update(null, updateWrapper);
        return updateStatus > 0 ? ResponseModel.success("修改部门信息成功") : ResponseModel.fail("修改部门信息失败");
    }


    /**
     * 部门详情
     */
    public ModelAndView deptDetail(String dept_id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dept/dept-detail");
        Map<String, Object> map = myDeptMapper.getDeptDetail(dept_id);
        if (Convert.toInt(map.get("parent_id")) == 0) {
            map.put("parentDeptName", "顶级部门");
        } else {
            MyDept parentDept = myDeptMapper.selectById(Convert.toInt(map.get("parent_id")));
            if (parentDept != null) {
                map.put("parentDeptName", parentDept.getDeptName());
            }
        }
        modelAndView.addAllObjects(map);
        return modelAndView;
    }


    /**
     * 删除部门
     */
    public ResponseModel deleteDept(int deptId){
        List<MyDept> deptList = myDeptMapper.selectList(new QueryWrapper<MyDept>().eq("parent_id", deptId));
        if (deptList.size() > 0) {
            return ResponseModel.fail("此部门有下级部门，不允许删除");
        }
        int delStatus = myDeptMapper.deleteById(deptId);
        return delStatus > 0 ? ResponseModel.success("删除部门信息成功") : ResponseModel.fail("删除部门信息失败");
    }
}

